//
//  CardDetailFootView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2019/1/2.
//  Copyright © 2019年 DUONIU_MAC. All rights reserved.
//

import UIKit

class CardDetailFootView: UIView {
    
    var model : CardDetailsModel! {
        didSet{
            
            updateData()
        }
    }
    
    let yellowView = UIView()
    fileprivate let imageView = UIImageView() //图片
    fileprivate let titleLabel = UILabel() //标题
    fileprivate let bankLabel = UILabel() //银行
    let touchButton = UIButton() //点击查看
    let amoutLabel = UILabel() //标签1
    fileprivate let discountLabel = UILabel() //标签2
    fileprivate let labelImage = UIImageView()
    fileprivate let labelImage2 = UIImageView()
    

    init() {
        super.init(frame: .zero)
        
        yellowView.backgroundColor = UIColor.drakYellow()
        self.addSubview(yellowView)
        
        yellowView.addSubview(imageView)
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        titleLabel.numberOfLines = 2
        yellowView.addSubview(titleLabel)
        
        bankLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        yellowView.addSubview(bankLabel)
        
        labelImage.image = UIImage.init(named: "select_green")
        yellowView.addSubview(labelImage)
        
        labelImage2.image = UIImage.init(named: "select_green")
        yellowView.addSubview(labelImage2)
        
        amoutLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        yellowView.addSubview(amoutLabel)
        
        discountLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        yellowView.addSubview(discountLabel)
        
        touchButton.backgroundColor = UIColor.red
        touchButton.setTitle(touchView, for: .normal)
        touchButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        yellowView.addSubview(touchButton)
        
        yellowView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.bottom.equalToSuperview()
        }

        
        imageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 30))
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.right.equalTo(self.snp.centerX).offset(RATIO(maxNum: -10))
            make.height.equalTo(RATIO_H(maxNum: 100))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.centerX).offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.height.greaterThanOrEqualTo(10)
            make.bottom.equalTo(imageView.snp.centerY).offset(RATIO_H(maxNum: -5))
        }
        
        bankLabel.snp.makeConstraints { (make) in
            make.left.right.equalTo(titleLabel)
            make.height.greaterThanOrEqualTo(10)
            make.top.equalTo(imageView.snp.centerY).offset(RATIO_H(maxNum: 5))
        }
        
        labelImage.snp.makeConstraints { (make) in
            make.top.equalTo(imageView.snp.bottom).offset(RATIO_H(maxNum: 30))
            make.width.height.equalTo(RATIO_H(maxNum: 20))
            make.left.equalTo(imageView).offset(RATIO(maxNum: 5))
        }
        
        amoutLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(labelImage)
            make.left.equalTo(labelImage.snp.right).offset(RATIO(maxNum: 5))
            make.right.equalTo(bankLabel)
            make.height.greaterThanOrEqualTo(10)
        }
        
        labelImage2.snp.makeConstraints { (make) in
            make.top.equalTo(labelImage.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.width.height.equalTo(labelImage)
        }
        
        discountLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(labelImage2)
            make.left.equalTo(labelImage2.snp.right).offset(RATIO(maxNum: 5))
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        touchButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(discountLabel)
        make.left.equalTo(discountLabel.snp.right).offset(RATIO(maxNum: 10))
            make.width.equalTo(RATIO(maxNum: 80))
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
    }
    
    //MARK: 刷新数据
    fileprivate func updateData() {
        
        imageView.setUrlImage(with: kmHttps + model.image!, placeholder: UIImage.imageWithName("loading"))
        titleLabel.text = model.credit_name
        bankLabel.text = KMTool.searchBankForId(id: model.bank_id!)
//        amoutLabel.text = model.card_type
        discountLabel.text = model.youhui1
        
        self.layoutIfNeeded()
        
        if touchButton.frame.maxX > yellowView.frame.maxX {
            
            touchButton.snp.remakeConstraints { (make) in
                make.centerY.equalTo(discountLabel)
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
                make.width.equalTo(RATIO(maxNum: 80))
                make.height.equalTo(RATIO_H(maxNum: 20))
            }
            
            discountLabel.snp.remakeConstraints { (make) in
                make.centerY.equalTo(labelImage2)
                make.left.equalTo(labelImage2.snp.right).offset(RATIO(maxNum: 5))
                make.right.equalTo(touchButton.snp.left).offset(RATIO(maxNum: -10))
                make.height.greaterThanOrEqualTo(10)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
